package Presentation.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JOptionPane;

import Data.BookMapper;
import Data.SellerMapper;
import Domain.Book;
import Domain.Seller;
import Presentationlayout.AddBookPanel;

/**
 * Gets book data from presentation layer and saves it to database with
 * BookMapper.
 * 
 * @author Johan Envik
 * 
 */

public class SaveBookAction implements ActionListener {
	private Book book = new Book();
	private Seller seller = new Seller();
	
	private AddBookPanel addBookPanel;
	

	/**
	 * Constructor for SaveBookAction.
	 * 
	 * @param addBookPanel
	 */
	public SaveBookAction(AddBookPanel addBookPanel) {
		this.addBookPanel = addBookPanel;
	}

	/**
	 * Method that gets all the book data from AddBookPanel and saves it to a
	 * new Book object. Then the Book object is sent to the BookMapper to be
	 * inserted into database.
	 * 
	 * At last the text fields are reseted.
	 */
	public void actionPerformed(ActionEvent arg0) {
		try {
			
			SellerMapper sMapper = new SellerMapper();
			String name = addBookPanel.getNameTextField().getText();
			String epost = addBookPanel.getEmailTextField().getText();
			String phonenr = addBookPanel.getPhoneTextField().getText();
			
			seller.setName(name);
			seller.setEmail(epost);
			seller.setPhonenr(phonenr);
			
			sMapper.insert(seller);
			
			BookMapper bMapper = new BookMapper();
			
			String title = addBookPanel.getTitleTextField().getText();
			String isbn = addBookPanel.getIsbnTextField().getText();
			String author = addBookPanel.getAuthorTextField().getText();
			String edition = addBookPanel.getEditionTextField().getText();
			String condition = (String) addBookPanel.getComboBoxCondition()
					.getSelectedItem(); // combobox
			String subject = (String) addBookPanel.getComboBoxSubjects()
					.getSelectedItem(); // combobox
			int price = Integer.parseInt(addBookPanel.getPriceTextField()
					.getText());
			
			System.out.println(title);
			System.out.println(isbn);
			System.out.println(author);
			System.out.println(edition);
			System.out.println(condition);
			System.out.println(subject);
			System.out.println(price);
			
			book.setTitle(title);
			book.setIsbn(isbn);
			book.setAuthor(author);
			book.setEdition(edition);
			book.setCondition(condition);
			book.setSubject(subject);
//			book.setDateCreated(new Date());
//			System.out.println(book.getDateCreated());
			book.setPrice(price);
			book.setIsSold(0);
			book.setSeller(seller);
		
			bMapper.insert(book);
			
			confirmAddDialog();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 addBookPanel.resetAllInputData();

	}
	public void confirmAddDialog() {
		JOptionPane.showMessageDialog(null, "Boken är nu inlagd.", "Bok inlagd", JOptionPane.INFORMATION_MESSAGE);
	}
}
